﻿using DotNet4.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;

namespace FridgeECSTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void ReadDominoTypeClick(object sender, RoutedEventArgs e)
        {
            LogDomino.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/DominoType/SelectAll", "");
        }

        private void AddDominoTypeClick(object sender, RoutedEventArgs e)
        {

        }

        private void ReadDominoClick(object sender, RoutedEventArgs e)
        {
            LogDomino.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/Domino/SelectAll", "");
        }

        private void AddDominoClick(object sender, RoutedEventArgs e)
        {
            
        }

        private void SelectPersonnelClick(object sender, RoutedEventArgs e)
        {
            LogPersonnel.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/Personnel?Method=SelectAll", "");
        }

        private void InsertPersonnelClick(object sender, RoutedEventArgs e)
        {
            var p1 = new Personnel { PersonnelUID = "DL-0000000003", ID = "ID1", Password = "Password1" };
            var p2 = new Personnel { PersonnelUID = "DL-0000000004", ID = "ID2", Password = "Password2" };
            var postData = JSON.stringify(new[] { p1, p2 });
            LogPersonnel.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/Personnel?Method=Insert", postData);
        }

        private void UpdatePersonnelClick(object sender, RoutedEventArgs e)
        {
            var p1 = new Personnel { PersonnelUID = "DL-0000000003", ID = "ID1", Password = "我是密码1" };
            var p2 = new Personnel { PersonnelUID = "DL-0000000004", ID = "ID2", Password = "我是密码2" };
            var postData = JSON.stringify(new[] { p1, p2 });
            LogPersonnel.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/Personnel?Method=Update", postData);
        }

        private void DeletePersonnelClick(object sender, RoutedEventArgs e)
        {
            var p1 = new Personnel { PersonnelUID = "DL-0000000003", ID = "ID1", Password = "Password1" };
            var p2 = new Personnel { PersonnelUID = "DL-0000000004", ID = "ID2", Password = "Password2" };
            var postData = JSON.stringify(new[] { p1, p2 });
            LogPersonnel.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/Personnel?Method=Delete", postData);
        }

        private void ReadPersonnelInformationClick(object sender, RoutedEventArgs e)
        {
            LogPersonnel.Text = C5HttpHelperPostResult("http://localhost:8080/FridgeECS/PersonnelInformation/SelectAll", "");
        }

        private void AddPersonnelInformationClick(object sender, RoutedEventArgs e)
        {




            //log(jsonString == JSON.stringify(new List<Person>() { p1, p2 }));   //true
            //log(jsonString);
            //// 反序列化，泛型集合
            //JSON.parse<List<Person>>(jsonString);
            //// 数组转换            
            //JSON.parse<Person[]>(jsonString);
        }

        private void UpdateDeviceStatusClick(object sender, RoutedEventArgs e)
        {
            LogFridgeAPI.Text = C5HttpHelperPostResult("http://malve-max.imwork.net/Fridge/UpdateDeviceStatus?DeviceUID=0", "");
        }

        private void UpdateDeviceStructureClick(object sender, RoutedEventArgs e)
        {
            LogFridgeAPI.Text = C5HttpHelperPostResult("http://malve-max.imwork.net/Fridge/UpdateDeviceStructure?DeviceUID=1", "");
        }

        private void UpdateShoppingClick(object sender, RoutedEventArgs e)
        {
            var p1 = "1,1,1,TOP";
            var p2 = "1,1,2,TOP";
            var postData = JSON.stringify(new[] { p1, p2 });
            LogFridgeAPI.Text = C5HttpHelperPostResult("http://malve-max.imwork.net/Fridge/UpdateShopping?DeviceUID=0&OpenID=OpenID-0", postData);
        }

        private String C5HttpHelperPostResult(string pUrl, string pPostData)
        {
            HttpHelper httpHelper = new HttpHelper();
            HttpItem httpItem = new HttpItem();

            httpItem.Method = "POST";

            httpItem.URL = pUrl;
            httpItem.Postdata = pPostData;

            HttpResult httpResult = httpHelper.GetHtml(httpItem);

            return httpResult.Html;
        }
    }

    public static class JSON
    {

        public static T parse<T>(string jsonString)
        {
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
            }
        }

        public static string stringify(object jsonObject)
        {
            using (var ms = new MemoryStream())
            {
                new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
    }
}
